//1. 导入 createSlice 函数
import {createSlice} from '@reduxjs/toolkit'

//2. 创建 slice 切片对象
let slice = createSlice({
  //状态的名称
  name: 'count',
  //状态的初始值
  initialState: 20, 
  //定义 reducer 内部的功能
  reducers: {
    //新增操作
    add: (state, action) => {
      return state + action.payload;
    },
    sub: (state, action) => {
      return state - action.payload;
    }
  }
});

//3. 通过切片对象, 获得同步的 action creator     () => {  return {type: 'incre', payload: 100}  }   
export let {add, sub} = slice.actions; 

//4. 通过切片对象, 获得当前状态的 reducer 函数
let reducer = slice.reducer;

//声明异步的 action creator
export function asyncAdd(payload){
  return dispatch => {
    setTimeout(() => {
      dispatch(add(payload));
    }, 1000)
  }
}

//暴露 reducer 函数
export default reducer;